﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProjectDescription : System.Web.UI.Page
{
    private static string _toAddress = "";

    private static string _path = "";
    private static int _projectId = 0;
    private static ProjectProberty pp;
    private static ProjectBLL pBll = new ProjectBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        _path = HttpContext.Current.Request.Url.AbsoluteUri;
        HttpCookie logincookie;
        AnswerBLL aBll = new AnswerBLL();
        
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(_path.Substring(_path.LastIndexOf('/') + 1)))
            {
                string endpath = _path.Substring(_path.LastIndexOf('/') + 1);
                if (Request.Cookies["descId" + endpath] != null)
                {
                    pp = new ProjectProberty();
                    HttpCookie cookie = Request.Cookies["descId" + _path.Substring(_path.LastIndexOf('/') + 1)];
                    _projectId = Convert.ToInt32(cookie.Value);
                    rpAnswers.DataSource = aBll.getAnswersByProjectID(_projectId);
                    rpAnswers.DataBind();
                    pp = pBll.GetProjectByID(_projectId);
                    lblTitle.Text = pp.Title;
                    lblName.Text = pp.ContactName;
                    lblCompanyName.Text = pp.CompanyName;
                    lblStartTime.Text = pp.StartPeriod.ToShortDateString();
                    lblEndTime.Text = pp.EndPeriod.ToShortDateString();
                    if (Request.Cookies["loginCookie"] != null)
                    {
                        logincookie = Request.Cookies["loginCookie"];
                        _toAddress = logincookie.Values["LoginMail"];
                    }
                    else
                    {
                        btnReserve.Visible = false;
                    }
                    if (pp.Reserved)
                    {
                        lblReserved.Text = "Er reserveret";
                        lblReserved.Visible = true;
                        btnReserve.Visible = false;
                    }
                    else
                    {
                        lblReserved.Visible = false;

                    }
                }
                //if (Request.Cookies["descId"+ ] != null)
                //{
                    
                //}
            }
        }
    }
    [WebMethod]
    protected void btnReserve_Click(object sender, EventArgs e)
    {
        if (Reserve())
        {
            lblReserved.Visible = true;
            
        }
    }
    protected void btnGoToFront_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }

    private bool Reserve()
    {
        bool canDo = false;
        if (!pp.Reserved)
        {
            pp.TimesReserved--;
            if (pp.TimesReserved == 0)
            {
                pp.Reserved = true;
                btnReserve.Visible = false;
                
            }
            if (pBll.Reservation(pp.ID, pp.TimesReserved, pp.Reserved) > 0)
            {
                //MailBLL mailService = new MailBLL();
                //mailService.SendMail(_toAddress, "Information", "Dette er en mail indeholdende info vedrørende det på valgte projekt.");
                canDo = true;
            }
            else
            {
                canDo = false;
            }
        }
        return canDo;
    }
    protected void btnBackward_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "goBack", "history.go(-2);", true);
    }
}